package club.tuding5;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * cos图爬虫
 * @author myd
 */
public class Main {
    public static void main(String[] args) throws Exception {
        //简单处理了一下源站图片列表
        String startPage = "http://47.92.139.173/COSxiezhen-2.htm";
        ArrayList articleList = new ArrayList();
        Document doc = Jsoup.connect(startPage).get();
        Elements articles = doc.getElementsByClass("excerpt-combine");
        for (Element el : articles) {
            final Elements e = el.select("h2 a");
            articleList.add(new HashMap<String, String>() {
                {
                    put("title", e.text());
                    put("src", e.attr("href"));
                }
            });
        }
        //获取图片列表完成，开始抓取图片
        for (Object article : articleList) {
            String articleSrc = (String) ((HashMap) article).get("src");
            String articleName = (String) ((HashMap) article).get("title");
            Document doc1 = Jsoup.connect(articleSrc).get();
            Elements content = doc1.getElementsByClass("article-content").select("p img");
            int counts = 0;
            for (Element el : content) {
                String picSrc = el.attr("src");
                DownloadUtil.get().download(picSrc, "/tmp/pics/" + articleName + "/", counts + ".jpg",
                        new DownloadUtil.OnDownloadListener() {
                            @Override
                            public void onDownloadSuccess(File file) {
                                System.out.println(file.getAbsoluteFile() + " 下载完成！");
                            }

                            @Override
                            public void onDownloadFailed(File file, Exception e) {
                                System.out.println(file.getAbsoluteFile() + " 下载失败！");
                                e.printStackTrace();
                            }
                        }
                );
                Thread.sleep(500);
                counts++;
            }
            counts = 0;
        }
    }
}
